package Geometry;

import java.util.ArrayList;
import java.util.List;

import android.graphics.Canvas;

/**
 * This will hold a set of pictures.
 * 
 * We can ask which picture contains a particular point and
 * 	get that picture.
 * 
 * We can draw all the pictures in this swarm.
 * 
 * 
 * @author bsetzer
 *
 */
public class Swarm {

	private List<Picture> components = new ArrayList<Picture>();
	//private Set<Picture> components = new HashSet<Picture>();
	
	/**
	 * Draws all the pictures in this swarm.
	 * 
	 * @param canv
	 */
	public  void draw(Canvas canv) {
		for(Picture p: components) {
			p.draw(canv);
		}
	}
	
	/**
	 * Finds a picture in this swarm containing the point
	 * 	(x,y) and returns it.
	 * If there is no such picture, return null.
	 * 
	 * @return  The picture containing the point (x,y), if
	 * 	there is one. 
	 * 	Returns null if no picture in this swarm contains 
	 * 	the point (x,y).
	 */
	public Picture findContaining(float x, float y) {
		// This search finds the component farthest in back that 
		// contains the point (x,Y).
//		for(Picture p : components) {
//			if(p.isInside(x, y)) {
//				return p;
//			} else {
//				// do nothing
//			}
//		}
		
		// Going backwards through the list means we find the 
		// 'front most' shape that contains the point (x,y).
		for(int i = components.size()-1; i >= 0; i--) {
			if(components.get(i).isInside(x, y))
				return components.get(i);
			else {
				// do nothing
			}
		}
		return null;
	}
	
	public void addPicture(Picture pic) {
		components.add(pic);
	}
	
	public void removePicture(Picture pic) {
		components.remove(pic);
	}
	
	public int getSize() {
		return components.size();
	}
	
}
